package com.lphoenix.pref_lab;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                                testCreatThread();
//                                testCreatBitmap();
//                                testCreatHeap();
//                testHttp();
            }
        });


    }

    private void testHttp() {
        //创建URL对象
        URL url = null;
        try {
            url = new URL("https://www.baidu.com");
            // 根据url 发送 http的请求
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置请求的方式
            conn.setRequestMethod("GET");
            //设置超时时间
            conn.setConnectTimeout(5000);
            // 得到服务器返回的响应码
            int code = conn.getResponseCode();
            //请求网络成功后返回码是200
            if (code == 200) {
                //获取输入流
                InputStream is = conn.getInputStream();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    int i = 0;
    List<Bitmap> list = new ArrayList<>();

    private void testCreatThread() {
        while (true) {
            Log.e("oom", "i..." + i++);
            try {
                if (i == 1801) {
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (i == 1800) {
                            Log.e("oom", "i..." + DeviceUtil.getProcData());
                        }
                        Thread.sleep(100000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "i..." + i).start();
        }

    }


    private void testCreatBitmap() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.e("oom", "i..." + i++);
            Bitmap m10 = BitmapFactory.decodeResource(getResources(), R.mipmap.img_10m);

            if (i == 8) {
                log(m10);
                String vmSize = DeviceUtil.getProcData();
            }
            list.add(m10);
        }
    }

    List<byte[]> bytesList = new ArrayList<>();

    private void testCreatHeap() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            Runtime.getRuntime().maxMemory()
            Log.e("oom", "i..." + i++);
            if(i == 50){
                DeviceUtil.getProcData();
            }
            bytesList.add(new byte[1024 * 1024 * 10]);
        }

    }

    private void log(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Log.e("oom", "getAllocationByteCount..." + bitmap.getAllocationByteCount() + "..getByteCount.."
                    + bitmap.getByteCount() + "..getRowBytes.." + bitmap.getRowBytes());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}